from ssh.terminal import *

hostnames = ['monitor', 'mng1', 'mng2', 'node1', 'node2', 'node3', 'node4', 'srv1', 'srv2', 'srv3']
# hostnames = ['monitor']


def name2host(hostnames, username='', password=''):
    hosts = []
    for hostname in hostnames:
        if username.isspace() or password.isspace():
            hosts.append(Host(hostname))
        else:
            hosts.append(Host(hostname, username, password))
    return hosts


if __name__ == '__main__':
    hosts = name2host(hostnames)
    group = TerminalGroup(hosts)
    group.exec("java -version")
    # executor.exec("hdfs --daemon start journalnode")
    # executor.exec("docker-compose -f /data/config/docker-compose.yaml up -d zookeeper")

# expect_next = [
#     {"expect": "Are you sure you want to continue connecting (yes/no)?", "next": "yes"},
#     {"expect": "password:", "next": "xiaoma"},
# ]
# executor.exec_with_expect("bash -c 'scp xiaoma@node1:/data/download/hadoop-3.2.1.tar.gz /data/download'", expect_next)

# 更新 Host 指令
# hosts_cmd = "echo '" \
#             "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n" \
#             "::1         localhost localhost.localdomain localhost6 localhost6.localdomain6\n\n" \
#             "192.168.1.241  monitor\n" \
#             "192.168.1.239  mng1\n" \
#             "192.168.1.242  mng2\n" \
#             "192.168.1.222  node1\n" \
#             "192.168.1.93   node2\n" \
#             "192.168.1.220  node3\n" \
#             "192.168.1.230  node4\n" \
#             "192.168.1.243  srv1\n" \
#             "192.168.1.244  srv2\n" \
#             "192.168.1.247  srv3" \
#             "' > /etc/hosts"